<template>
	<view class="management_case">
		<!-- current_handing_type == 4案件处置  3 案件回退    6 申请延期缓办    9监理待核查-->
		<view class="flex align-center mb20"  v-if="current_handing_type == 4  ||  current_handing_type == 9" >
			<view class="">案件位置：</view>
			<view class="address">
				<input type="text" value="" placeholder="请输入案件位置" v-model="currentAddress" class="f14" />
				<image src="../../static/images/bg_03.png" mode="" class="images" @click="getCurretAddress"></image>
			</view>	
		</view>
		<!-- bdjgjxy    6 申请 -->
		<view class="flex align-center mb20"  v-if="current_handing_type == 6" >
			<view class="">申请类型：</view>
			<picker @change="getSubclass" :range="categoryNameData" :value='Id' class="picker_select" range-key="name">
				<text class="uni-input">{{eventname}} </text>
				<image class="drop-down" src="../../static/images/shangbao_07.png"></image>
			</picker>
		</view>
		<view class="uni-back flex" v-if="current_handing_type == 6">
			<view>申请说明：</view>
			<textarea value="" placeholder="请输入申请说明" v-model="eventDescribe" maxlength="250"/>
		</view>
		<view class="uni-back flex" v-else>
			<view>案件描述：</view>
			<textarea value="" placeholder="请输入案件描述" v-model="eventDescribe" maxlength="250"/>
		</view>
		<!-- bdjgjxy 0缓办不显示 1延期-->
		<view class="uniCanal"  v-if="current_handing_type == 6 && Id == 1">
			<view>附件上传:</view>
			<view class="uni-Camera">
				<view class="imageAll"  v-if="imagesArr && imagesArr.length > 0">
					<view class="image-box" v-for="(item, index) in imagesArr" :key="index">
						<image class="selectImg" :src="item" mode="aspectFit" alt="" @click="showImage(item)">
							<i class="closeImg" @click="deleteImg(index)"></i>
					</view>
				</view>
				<view class="uni-Camera-zero" @click="takePictures"  v-if="imagesArr.length<3">+</view>
			</view>
		</view>
		<view class="uniCanal" v-if="current_handing_type != 6">
			<view class="uni-Camera">
				<view class="imageAll"  v-if="imagesArr && imagesArr.length > 0">
					<view class="image-box" v-for="(item, index) in imagesArr" :key="index">
						<image class="selectImg" :src="item" mode="aspectFit" alt="" @click="showImage(item)">
							<i class="closeImg" @click="deleteImg(index)"></i>
					</view>
				</view>
				<view class="uni-Camera-zero" @click="takePictures"  v-if="imagesArr.length<3">+</view>
			</view>
			<view class="smallWarn">
				*请在拍照时，确保事件的环境位置关系与拍摄全景，如一张照片无法表现时，可拍摄多张！
			</view>
		</view>
		<view class="flex buttons" v-if="current_handing_type == 3||current_handing_type == 4">
			<view  @click="showAlert">提交</view>
		</view>
		<view class="flex buttons" v-if="current_handing_type == 6">
			<view  @click="showAlert1">提交</view>
		</view>
		<view class="flex setBtnTwo" v-if="current_handing_type == 9">
			<view class="red"  @click="goReturn(0)">驳回</view>
			<view  @click="goReturn(1)">回转</view>
		</view>
	</view>
</template>

<script>
	import {globalTime,getTodayTime} from '../../common/getTimeqj.js'
	import {getEvenCode,Dealevent,geteventSlow,getEventbzy,geteventDelay,geteventhbSlow} from '../../util/api.js'
	import URL from '../../util/url.js'
	export default {
		data() {
			return{
				userInfor: uni.getStorageSync('userInfor'),
				imagesArr: [],
				
				eventId: '',
				//案件的坐标
				gdx: '',
				gdy: '',
				//案件描述
				eventDescribe: '',
				//案件图片
				paramsImages: [],
				//当前处置人员位置
				currentx: '',
				currenty: '',
				currentAddress: '',
				//判断当前是从哪进的
				current_handing_type: '',
				//秒数 中心
				typeCode:'',
				eventItem:'',
				categoryNameData:[],
				eventname:'',
				Id:'',
			}
		},
		onLoad(options) {
			//判断当前是从哪进的
			this.current_handing_type = options.current_handing_type;
			this.eventItem = JSON.parse(options.eventItem)
			this.currentAddress = '';
			//判断是延期还是缓办
			if(this.eventItem.archivesTypeLeveThree.isYq&&this.eventItem.archivesTypeLeveThree.isYq==1&&
			this.eventItem.archivesTypeLeveThree.isHb&&this.eventItem.archivesTypeLeveThree.isHb==1){
				this.categoryNameData = [{name:'缓办',id:'0'},{name:'延期',id:'1'}];
				this.eventname = '缓办';
				this.Id='0';
			}else if(this.eventItem.archivesTypeLeveThree.isYq&&this.eventItem.archivesTypeLeveThree.isYq==1){
				this.categoryNameData = [{name:'延期',id:'1'}];
				this.eventname = '延期';
				this.Id='1';
			}else if(this.eventItem.archivesTypeLeveThree.isHb&&this.eventItem.archivesTypeLeveThree.isHb==1){
				this.categoryNameData = [{name:'缓办',id:'0'}];
				this.eventname = '缓办';
				this.Id='0';
			}
			console.log(this.categoryNameData)
			//案件位置
			this.eventId = options.eventId;
			this.gdx = options.gdx;
			this.gdy = options.gdy;
			//获取指挥中心描述
			if (this.current_handing_type == 3) {
				uni.setNavigationBarTitle({
					title: '案件回退'
				});
			}else if (this.current_handing_type == 4) {
				uni.setNavigationBarTitle({
					title: '案件处置'
				});
			}else if (this.current_handing_type == 6) {
				uni.setNavigationBarTitle({
					title: '申请'
				});
			}else if (this.current_handing_type == 9) {
				uni.setNavigationBarTitle({
					title: '案件核查'
				});
			}
		
		},
		onShow() {
			//处置人员当前位置信息
			let currentLocation = uni.getStorageSync('currentLocation1');
			this.currentx = currentLocation.currentx||'108.94866955566405';
			this.currenty = currentLocation.currenty||'34.21406493345206';
			this.currentAddress = currentLocation.currentAddress||'';
		},
		methods:{
			getSubclass(e){
				console.log(e)
				this.Id = this.categoryNameData[e.detail.value].id;
				this.eventname = this.categoryNameData[e.detail.value].name;
				//判断缓办不显示上传图片 0缓办 1延期 这里判断缓办的时清空照片
				if(this.Id == 0){
					this.imagesArr=[];
					this.paramsImages=[];
				}
			},
			//提交 核实 与核查都是一个接口
			showAlert() {
				if (this.current_handing_type == 4) {
					if (this.currentAddress == '' || this.currentAddress == undefined) {
						this.$toast.showToast('请输入案件位置');
						return;
					}
				}
				if (this.current_handing_type == 3 || this.current_handing_type == 4) {
					if (this.paramsImages.length == 0) {
						this.$toast.showToast('请选择图片或进行拍照');
						return;
					}
				}
				if (this.current_handing_type == 3 || this.current_handing_type == 4) {
					if (this.eventDescribe == '') {
						this.$toast.showToast('请输入描述内容');
						return;
					}
				}
				let _this = this,
					options = {},
					url = null;
				var images = JSON.stringify(this.paramsImages);
				if (this.current_handing_type == 3 ||this.current_handing_type == 4) {
					url = Dealevent;
					options = {
						eventId:this.eventId,
						createUser:this.userInfor.userId,
						remark:this.eventDescribe,
						eventAddress:this.currentAddress,
						beforeImg:images,
						organizationId:this.userInfor.organizationId,
						eventDealStatus:this.eventItem.eventDealStatus,
						eventStatus:this.eventItem.eventStatus,
						isBack:this.current_handing_typ==3?1:0,   //  0处置  1 回退
					}
				}else if(this.current_handing_type == 6){
					url = geteventSlow;
					var todayTime = getTodayTime();
					options = {
						eventId:this.eventId,
						createUser:this.userInfor.userId,
						organizationId:this.userInfor.organizationId,
						remark:this.eventDescribe,
						beforeImg:this.Id==1?images:'',
						isBack:this.Id,   //1延期  0缓办 
						eventDealStatus:this.eventItem.eventDealStatus,
						createDate:todayTime,  //申请时间
					}
				}
				uni.showLoading();
			
				url(options).then((res) => {
					uni.hideLoading();
					if (res.code === 200) {
						this.$toast.showToast('提交成功');
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/pageList/pageList'
								// url:'/pages/detailPage/detailPage?eventItem='+JSON.stringify(this.eventItem)
							})
						}, 1000)
					} else {
						this.$toast.showToast(res.msg);
					}
				}).catch((error) => {
					uni.hideLoading();
				})
			},
			//	//如果等于6 先判断一下是否有权限在调用提交接口  this.Id == 1延期   0缓办
			// tglist:申请了多少次延期一个月不能超过10次  isDelay预计处理时间0未超时1已超时  
			//wslist 延期中0可以延期 大于0延期中  isJj 为1不能延期 isTs 为1 特殊案件不能延期
			showAlert1(){
				if (this.eventDescribe == '') {
					this.$toast.showToast('请输入申请说明');
					return;
				}
				if (this.Id == 1&&this.paramsImages.length == 0) {
					this.$toast.showToast('请选择图片或进行拍照');
					return;
				}
				var Url = null;
				var textWord= '';
				if(this.Id == 1){
					Url = geteventDelay;
					textWord = '延期';
				}else if(this.Id == 0){
					Url = geteventhbSlow;
					textWord = '缓办';
				}
				let parmes = {eventId:this.eventId}
				Url(parmes).then((res)=>{
					if(res.data.isDelay == 1||res.data.isTs == 1||res.data.isJj == 1||res.data.wsList > 0||res.data.tgList >= 10){
						if(res.data.isDelay == 1){
							this.$toast.showToast('案件超时暂不能'+textWord)
						}
						if(res.data.isTs == 1){
							this.$toast.showToast('特殊案件暂不能'+textWord)
						}
						if(res.data.isJj == 1){
							this.$toast.showToast('紧急案件暂不能'+textWord)
						}
						if(res.data.wsList > 0){
							this.$toast.showToast('有已申请的未处理的案件暂不能'+textWord)
						}
						if(res.data.tgList >= 10){
							this.$toast.showToast('超过10次暂不能'+textWord)
						}
					}else{
						this.yanqi();
					}
				}).catch((err)=>{
					uni.hideLoading();
				})
			},
			yanqi(){
				let _this = this,
					options = {},
					url = null;
				var images = JSON.stringify(this.paramsImages);
				if(this.current_handing_type == 6){
					url = geteventSlow;
					var todayTime = getTodayTime();
					options = {
						eventId:this.eventId,
						createUser:this.userInfor.userId,
						organizationId:this.userInfor.organizationId,
						remark:this.eventDescribe,
						beforeImg:this.Id==1?images:'',
						isBack:this.Id,   //1延期  0缓办 
						eventDealStatus:this.eventItem.eventDealStatus,
						createDate:todayTime,  //申请时间
					}
				}
				uni.showLoading();
				url(options).then((res) => {
					uni.hideLoading();
					if (res.code === 200) {
						this.$toast.showToast('提交成功');
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/pageList/pageList'
								// url:'/pages/detailPage/detailPage?eventItem='+JSON.stringify(this.eventItem)
							})
						}, 1000)
					} else {
						this.$toast.showToast(res.msg);
					}
				}).catch((error) => {
					uni.hideLoading();
				})
			},
			//0审核 驳回  1审核回转
			goReturn(type){
				if (this.currentAddress == '' || this.currentAddress == undefined) {
					this.$toast.showToast('请输入案件位置');
					return;
				}
				if (this.eventDescribe == '') {
					this.$toast.showToast('请输入描述内容');
					return;
				}
				if (this.paramsImages.length == 0) {
					this.$toast.showToast('请选择图片或进行拍照');
					return;
				}
				var images = JSON.stringify(this.paramsImages);
				var option = {
					eventId:this.eventId,
					createUser:this.userInfor.userId,
					isBack:type,  //o驳回1回转
					// organizationId:this.userInfor.organizationId,
					remark:this.eventDescribe,
					beforeImg:images,
					eventDealStatus:this.eventItem.eventDealStatus,
				}
				uni.showLoading();
				getEventbzy(option).then((res) => {
					uni.hideLoading();
					if (res.code === 200) {
						this.$toast.showToast('提交成功');
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/pageList/pageList'
								// url:'/pages/detailPage/detailPage?eventItem='+JSON.stringify(this.eventItem)
							})
						}, 1000)
					} else {
						this.$toast.showToast(res.msg);
					}
				}).catch((error) => {
					uni.hideLoading();
				})
			},
			getDescrad(){
				var parse={
					eventId:this.eventId,
					typeCode:this.typeCode,
				}
				getEvenCode(parse).then((res)=>{
					if(res.code == 200){
						this.remark = res.data.remark
					}else{
						this.$toast.showToast(res.msg)
					}
				}).catch((err)=>{
					console.log(err)
				})
			},
			getCurretAddress() {
				uni.navigateTo({
					url: '/pages/detailPage/pendingCaseMap?gdx=' + this.gdx + '&gdy=' + this.gdy + '&mtype=2'
				})
			},
			//调手机相册或相机
			takePictures() {
				let _this = this;
				// 选择图片
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera','album'],
					success: function(res) {
						uni.showLoading({
						    title: '正在上传图片',
							mask: true
						});
						_this.imagesArr.push(res.tempFilePaths[0]);
						uni.uploadFile({
							header:{
								'Authorization': 'bearer ' + uni.getStorageSync('token'),
							},
							url: URL.Upload1,
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: function(uploadFileRes) {
								console.log(uploadFileRes)
								uni.hideLoading();
								let res = JSON.parse(uploadFileRes.data);
								if (res.code === 200) {
									_this.paramsImages.push({
										"image": URL.IMAGEURL1 + res.fileName,
									});
								}else {
									this.$toast.showToast('请重新拍照');
								}
							},
							fail(err) {
								console.log(err)
								uni.hideLoading();
							}
						})
					}
				});
			},
			//点击图片放大
			showImage(item) {
				let _this = this;
				uni.previewImage({
					urls: _this.imagesArr,
					current: item,
					longPressActions: {
						//itemList: ['发送给朋友', '保存图片', '收藏'],//按钮的文字数组
						success: function(data) {},
						fail: function(err) {}
					}
				});
			},
			// 删除本地图片：未上传
			deleteImg(index) {
				this.imagesArr.splice(index, 1);
				this.paramsImages.splice(index, 1);
			},
		}
	}
</script>

<style scoped lang="scss">
	.management_case {
		height: calc(100% - 48rpx);
		margin: 0 auto;
		padding: 30rpx;
		background: #fff;
		picker{
			flex: 1;
		}
		.address{
			border: 1px solid #C0C0C0;
			display: flex;
			flex: 1;
			color: #919293;
			padding: 5px;
			border-radius: 5px;
			position: relative;
			.images{
				position: absolute;
				right: 10px;
				width: 19px;
				height: 20px;
			}
			input{
				flex: 1;
				padding-right: 50rpx;
			}
		}
		.uniCanal{
			margin-bottom: 20rpx;
			.smallWarn{
				color: #B85F5B;
				font-size: 12px;
				margin-top: 10rpx;
			}
			.uni-Camera {
				width: 100%;
				display: flex;
				margin-top: 10rpx;
				.imageAll{
					display: flex;
					.image-box{
						width: 145rpx;
						height: 145rpx;
						border: 1px solid #C0C0C0;
						margin-right: 10px;
						display: flex;
						align-items: center;
						justify-content: center;
						position: relative;
						&:nth-of-type(3n) {
							margin-right: 0;
						}
						
						uni-image {
							width: 100%;
							height: 100%;
						}
						
						.closeImg {
							//带_的是微信小程序的样式
							position: absolute;
							top: -6px;
							right: -6px;
							width: 16px;
							height: 16px;
							background: red;
							border-radius: 50%;
							line-height: 13px;
							text-align: center;
						
							&:before {
								content: '×';
								color: #fff;
							}
						}
					}
				}
				.uni-Camera-zero {
					width: 145rpx;
					height: 145rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					border: 1px solid #C0C0C0;
					font-size: 40px;
				}
			}
		}
		.uni-back{
			margin-bottom: 30rpx;
			textarea{
				flex: 1;
				margin-top: 10rpx;
				border: 1px solid #C0C0C0;
				font-size: 14px;
			}
		}
		.buttons{
			position: absolute;
			bottom: 100rpx;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			view{
				width: 356rpx;
				height: 90rpx;
				border-radius: 10rpx;
				font-size: 16px;
				background-color: #4A8CEF;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #FFFFFF;
			}
		}
		.setBtnTwo{
			position: absolute;
			bottom: 100rpx;
			left: 30rpx;
			right: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
			view{
				width: 278rpx;
				height: 82rpx;
				border-radius: 20rpx;
				font-size: 16px;
				background-color: #4A8CEF;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #FFFFFF;
			}
			.red{
				background-color: #D34344;
			}
		}
	}
</style>
